<template>
  <div class="keyWork-detail-wrap">
    <Navbar title="工作详情" />
    <div class="list-content">
      <div class="top">
        <p class="name">{{detailData.workTitle}}</p>
        <labelType v-if="detailData.workStatus" :type="detailData.workStatus"></labelType>
        <p class="unit">主办单位：{{detailData.deptName}}</p>
        <p class="time">完成时限：{{detailData.completionDdl}}</p>
        <div class="remain-time flex-center" v-if="detailData.workStatus == '进行中'||detailData.workStatus == '超期'">
          <img src="@/assets/keyWork/icon-remainTime.png" alt="">
          <span v-if="detailData.workStatus == '进行中'">剩余{{detailData.daysRemaining}}天</span>
          <span v-if="detailData.workStatus == '超期'">超期{{detailData.daysRemaining}}天</span>
        </div>
      </div>
      <div class="content">
        <div class="module">
          <div class="title">工作内容和任务目标</div>
          <div class="text">
            {{detailData.workDetail || "--"}}
          </div>
        </div>
        <div class="module">
          <div class="title">上季度工作进展</div>
          <div class="text">
            {{detailData.workProcess || "--"}}
          </div>
        </div>
        <div class="module error">
          <div class="title">存在问题</div>
          <div class="text">
            {{detailData.workProblem || "--"}}
          </div>
        </div>
        <div class="module">
          <div class="title">下一步工作计划</div>
          <div class="text">
            {{detailData.nextStep || "--"}}
          </div>
        </div>
      </div>
    </div>
    <div class="op-btn-wrap">
      <div class="btn" @click="handleContact">联系责任人</div>
    </div>

    <van-dialog
      title="请选择联系人"
      v-model:show="contactVisible"
      show-cancel-button 
      :before-close="beforeClose"
    >
      <van-checkbox-group v-model="result" direction="horizontal" ref="checkboxGroup" @change="checkChange">
        <van-checkbox
          use-icon-slot
          v-for="(item,index) in userList"
          :key="index"
          :value="item.name"
          :name="item.name"
        >
          {{item.name}}
        </van-checkbox>
      </van-checkbox-group>
    </van-dialog>
  </div>  
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import Navbar from '@/components/Navbar/index.vue'
import labelType from "./labelType.vue"
import { showToast } from 'vant'
import { useContactStore } from '@/store'
import keyWorkServe from '@/api/keyWork.ts'
import { useRouter, useRoute } from 'vue-router'
import dayjs from 'dayjs'

const route = useRoute()
const router = useRouter()

const detailData = ref({})
const contactVisible = ref(false)
const result = ref([])

const userList = ref([])
const userIds = ref([])


const getDetail = () => {
  const { id } = route.query
  keyWorkServe.focusWorkInfo(id).then(res=>{
    detailData.value = res.data
  })
}
getDetail()

const handleContact = () => {
  userIds.value = filterArray('personLiable')
  userList.value = []
  console.log("🚀 ~ file: detail.vue:99 ~ handleContact ~ userIds.value:", userIds.value)
  if(userIds.value.length>1){
    let deptNames = filterArray('deptName')
    let deptIds = filterArray('deptId')
    deptNames.forEach((ele,index)=>{
      userList.value.push({
        name: ele,
        value: deptIds[index]
      })
    })
    contactVisible.value = true
  }else{
    showPickerClick(userIds.value[0])
  }
}

const filterArray = (key) => {
  let idList = detailData.value[key].split(';')
  let ids = idList.filter((s) => {
    return s && s.trim();
  });
  return ids
}

const checkChange = (val: any) => {
  
}

const onClose = () => {
  contactVisible.value = false
}

const beforeClose = (action: String) => {
  if(action === 'confirm'){
    if(result.value.length>0){
      onClose()
      showPickerClick(userIds.value)
    }else{
      showToast('请先选择联系人')
    }
  }else{
    onClose()
  }
}

const contactStore = useContactStore()
const showPickerClick = (ids: any) =>{
  contactStore.fetchUserDetail(ids)
}
</script>

<style scoped lang="less">
.keyWork-detail-wrap{
  .list-content{
    height: calc(100vh - 50px);
    overflow-y: scroll;
  }
  .top{
    padding: 18px 16px;
    background: #fff url("@/assets/keyWork/detail-bg.png") no-repeat 315px 12px;
    background-size: 52px 61px;
    color: #21212199;
    font-size: 13px;
    position: relative;
    line-height: 22px;

    .name{
      color: #212121;
      font-weight: bold;
      font-size: 16px;
      line-height: 26px;
      margin-bottom: 10px;
    }
    .unit{
      margin: 10px 0 1px;
    }
    .remain-time{
      position: absolute;
      right: 0;
      bottom: 20px;
      height: 29px;
      padding: 0 15px 0 10px;
      border-radius: 15px 0px 0px 15px;
      color: #FF4935;
      background: linear-gradient(90.55deg, #FFDCDEED 0%, #FFECED 0%, #FFDDDFED 100%);
      img{
        width: 12px;
        height: 12px;
        margin-right: 2px;
      }
    }
  }
  .content{
    padding: 0px 12px 70px;
    .module{
      border-radius: 8px;
      background: #FFFFFF;
      border: 1px solid #EEF4FD;
      margin-top: 12px;
      padding: 12px 11px;
      .title{
        color: #212121;
        font-weight: bold;
        font-size: 16px;
        line-height: 16px;
        background: url("@/assets/keyWork/icon-detail-title.png") no-repeat left 2px;
        background-size: 8px 16px;
        padding-left: 12px;
        margin-bottom: 12px;
      }
      .text{
        border-radius: 6px;
        background: linear-gradient(180deg, #EEF4FD 0%, #EEF4FD00 100%);
        padding: 14px 12px 10px;
        color: #212121CC;
        font-size: 14px;
        line-height: 23px;
      }
    }
    .error{
      .title{
        background: url("@/assets/keyWork/icon-error-title.png") no-repeat left center;
        background-size: 19.5px 20px;
        padding-left: 22px;
      }
      .text{
        background: linear-gradient(180deg, rgba(255, 73, 53, 0.05) 0%, rgba(255, 73, 53, 0) 100%);
      }
    }
  }
  .op-btn-wrap{
    position: fixed;
    bottom: 0px;
    left: 0;
    right: 0;
    .btn {
      border-radius: 22px;
      height: 44px;
      width: 320px;
      margin: 8px auto;
      text-align: center;
      line-height: 44px;
      background: #3a75c5;
      color: #ffffff;
      font-size: 16px;
    }
  }
  :deep(.van-popup){
    .van-dialog__header{
      padding: 26px 0;
      background: url("@/assets/keyWork/contact-bg.png") no-repeat center center;
      background-size: 232px 2.5px;
    }
    .van-dialog__content{
      padding: 3px 36px 15px;
      max-height: 140px;
      overflow-y: auto;
      .van-checkbox{
        width: 100%;
        height: 40px;
        border-radius: 4px;
        padding: 0 10px;
        margin: 0 0 4px 0;
        .van-checkbox__label{
          width: 100%;
          text-align: center;
          padding-right: 12px;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        .van-checkbox__icon{
          height: auto;
          opacity: 0;
          .van-icon{
            width: 16px;
            height: 16px;
          }
        }
        &[aria-checked^="true"]{
          background: #EEF4FD;
          .van-checkbox__icon{
            opacity: 1;
            .van-icon{
              background-color: #3A75C5;
              border-color: #3A75C5;
              font-size: 11px;
            }
          }
        }
      }
    }
    .van-dialog__footer{
      width: 260px;
      margin: 15px auto 28px;
      .van-button {
        width: 120px;
        height: 44px;
        line-height: 44px;
        border-radius: 25px;
        font-weight: bold;
        font-size: 16px;
      }
      .van-dialog__cancel{
        border: 1px solid #3A75C5;
        box-shadow: 0px 2px 5px 0px #00000019;
        color: #3A75C5;
        margin-right: 20px;
      }
      .van-dialog__confirm {
        background: #3A75C5;
        box-shadow: 0px 2px 5px 0px #00000019;
        color: #fff;
      }
    }
  }
}
</style>
